//
//  JKView.m
//  JKMVPTest
//
//  Created by 王冲 on 2018/9/29.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "JKView.h"
#import "KVOController.h"
@interface JKView ()

@property(strong,nonatomic) UILabel *label;

@end

@implementation JKView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self addSubview:self.label];
    }
    
    return self;
}

#pragma mark 随时监听viewModel的变化给JKView的控件赋值
-(void)setViewModel:(JKViewModel *)viewModel{
    
    _viewModel = viewModel;
    
    __weak typeof(self) weakSelf = self;
    [self.KVOController observe:self.viewModel keyPath:@"name" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change)
     {
         weakSelf.label.text = change[NSKeyValueChangeNewKey];
     }];
    
}

-(UILabel *)label{
    
    if (!_label) {
        
        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _label.textColor = [UIColor purpleColor];
        _label.font = [UIFont systemFontOfSize:20];
        _label.textAlignment = NSTextAlignmentCenter;
    }
    
    return _label;
}

@end
